package pl.bizuteria.sklep.admin.client.activity.product;

import pl.bizuteria.sklep.admin.client.ui.product.IProductEditView;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.web.bindery.requestfactory.shared.EntityProxy;
import com.google.web.bindery.requestfactory.shared.EntityProxyId;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.RequestContext;
import com.google.web.bindery.requestfactory.shared.RequestFactory;

public abstract class FindAndEditActivity<P extends EntityProxy, V extends IProductEditView<P>> extends EditActivity<P, V> {

	private final EntityProxyId<P> proxyId;
	protected P proxy;
	RequestFactory factory;

	public FindAndEditActivity(IRequestMixin<P> requestMixin, IProductEditView<P> view, EntityProxyId<P> proxyId,
			EventBus eventBus, PlaceController placeController,
			RequestContext request, RequestFactory factory) {
		super(requestMixin, view, eventBus, placeController, request);
		this.proxyId = proxyId;
		this.factory = factory;
	}

	public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
		Receiver<P> receiver = new Receiver<P>() {

			@Override
			public void onSuccess(P response) {
				proxy = response;
				FindAndEditActivity.super.start(panel, eventBus);
			}
		};

		factory.find(proxyId).fire(receiver);
	}
	
	@Override
	protected P getProxy() {
		return proxy;
	}

}
